import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*class jdbc{
    public void a() throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/bit?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("200456zjr");

        Connection connection = dataSource.getConnection();

        String sql = "select * from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,2);

        ResultSet resultSet = statement.executeQuery();//返回的集合

        while(resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println(id + name);
        }

        resultSet.close();
        statement.close();
        connection.close();
    }
    public void select() throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/bit?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("200456zjr");

        Connection connection = dataSource.getConnection();

        String sql = "select * from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,2);

        ResultSet resultSet = statement.executeQuery();//返回的集合

        while(resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println(id + name);
        }

        resultSet.close();
        statement.close();
        connection.close();
    }
    public int insert(int id, String name) throws SQLException {
        //1.创建DataSource
        DataSource dataSource = new MysqlDataSource();
        //URL代表是网络上的位置(网址) - IP: 127.0.0.1, 端口号: 3306, bit: mysql中创建的库, utf8: 字符集, useSSL是否加密
        // ?后面代表的是 访问资源需要的一些参数
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/bit?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");//root:以管理员的身份运行
        ((MysqlDataSource)dataSource).setPassword("200456zjr");//密码

        //2.和数据库服务器建立连接
        Connection connection = dataSource.getConnection();

        //3. 构造 sql
        //String sql = "insert into student values(1,'张三')";
        //String sql = "delete from student where id = 1";
        //String sql = "update student set name = 'x' where id = 2";
        String sql = "insert into student values(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);//替换第几个?
        statement.setString(2,name);
        System.out.println(statement);


        //4.把 sql 发给服务器
        //statement.executeQuery(); - select
        int n = statement.executeUpdate();//返回影响到的长度 - 其他
        System.out.println(n);

        //5.释放资源
        statement.close();
        connection.close();
        return n;
    }
    public int delete(int id) throws SQLException {
        //1.创建DataSource
        DataSource dataSource = new MysqlDataSource();
        //URL代表是网络上的位置(网址) - IP: 127.0.0.1, 端口号: 3306, bit: mysql中创建的库, utf8: 字符集, useSSL是否加密
        // ?后面代表的是 访问资源需要的一些参数
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/bit?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");//root:以管理员的身份运行
        ((MysqlDataSource)dataSource).setPassword("200456zjr");//密码

        //2.和数据库服务器建立连接
        Connection connection = dataSource.getConnection();
        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);

        int n = statement.executeUpdate();
        statement.close();
        connection.close();
        return n;
    }
    public int update(int id, String name) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/bit?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("200456zjr");
        Connection connection = dataSource.getConnection();
        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(2,id);
        statement.setString(1,name);

        int n = statement.executeUpdate();
        statement.close();
        connection.close();
        return n;
    }
}*/
public class Test {
    public static void main(String[] args) throws SQLException {
        jdbc a = new jdbc("root","200456zjr","bit");

        a.update(5, "ab");
        a.select();
    }
}
class jdbc{
    String user;
    String password;
    Connection connection;
    PreparedStatement statement;
    String sql;
    DataSource dataSource;
    public jdbc(String user, String password, String lib) throws SQLException {
        this.user = user;
        this.password = password;
        dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/"+ lib +"?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser(user);
        ((MysqlDataSource)dataSource).setPassword(password);

    }
    public void select() throws SQLException {
        connection = dataSource.getConnection();
        sql = "select * from student";
        statement = connection.prepareStatement(sql);

        ResultSet resultSet = statement.executeQuery();

        while (resultSet.next()){
            int Id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println(Id + " " + name);
        }
        resultSet.close();
    }
    public int insert(int id, String name) throws SQLException {
        connection = dataSource.getConnection();
        sql = "insert into student values(?,?)";
        statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);

        int n = statement.executeUpdate();
        return n;
    }
    public int delete(int id) throws SQLException {
        connection = dataSource.getConnection();
        sql = "delete from student where id = ?";
        statement = connection.prepareStatement(sql);
        statement.setInt(1,id);

        int n = statement.executeUpdate();
        return n;
    }
    public int update(int id, String name) throws SQLException {
        connection = dataSource.getConnection();
        sql = "update student set name = ? where id = ?";
        statement = connection.prepareStatement(sql);
        statement.setInt(2,id);
        statement.setString(1,name);

        int n = statement.executeUpdate();
        return n;
    }
    public void close() throws SQLException {
        statement.close();
        connection.close();
    }
}
class Solution {
    public int subtractProductAndSum(int n) {
        return mul(n)-sum(n);
    }
    private int mul(int n){
        if(n<10){
            return n;
        }
        return mul(n/10)*(n%10);
    }
    private int sum(int n){
        if(n<10){
            return n;
        }
        return sum(n/10)+n%10;
    }
}